package com.joysuch.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillWrapper;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class ExcelUtil {

    private ExcelUtil(){}

    public static <T> List<T> readExcel(String path, Class<T> clazz, int sheetIndex, int headRow) {
        List<T> workHourList;
        try (ExcelReader excelReader = EasyExcel.read(path).build()) {
            ExcelListener<T> listener = new ExcelListener<>();
            ReadSheet readSheet = EasyExcel.readSheet(sheetIndex).head(clazz).registerReadListener(listener).headRowNumber(headRow).build();
            excelReader.read(readSheet);
            workHourList = listener.getDatas();
        }
        return workHourList;
    }

    public static <T> List<T> readExcel(String path, Class<T> clazz, String sheetName, int headRow) {
        List<T> workHourList;
        try (ExcelReader excelReader = EasyExcel.read(path).build()) {
            ExcelListener<T> listener = new ExcelListener<>();
            ReadSheet readSheet = EasyExcel.readSheet(sheetName).head(clazz).registerReadListener(listener).headRowNumber(headRow).build();
            excelReader.read(readSheet);
            workHourList = listener.getDatas();
        }
        return workHourList;
    }

    public static <T> List<T> readExcel(File path, Class<T> clazz, String sheetName, int headRow) {
        List<T> workHourList;
        try (ExcelReader excelReader = EasyExcel.read(path).build()) {
            ExcelListener<T> listener = new ExcelListener<>();
            ReadSheet readSheet = EasyExcel.readSheet(sheetName).head(clazz).registerReadListener(listener).headRowNumber(headRow).build();
            excelReader.read(readSheet);
            workHourList = listener.getDatas();
        }
        return workHourList;
    }

//    public static void writeExcel(List list, int sheetNo, InputStream templeteFile, String outPath, String excelName) {
////        // 输出路径
////        String outPath = "C:/ExcelHandle";
////        String excelFileName = outPath + "/" + DateUtils.format(new Date(), "yyyyMMdd_HHmmss") + "处理报表.xlsx";
//        String excelFileName = outPath + excelName;
//        File outputFolder = new File(outPath);
//        if (!outputFolder.exists()) {
//            boolean mkdirs = outputFolder.mkdirs();// 如果文件夹不存在，则创建
//        }
//        try(ExcelWriter excelWriter = EasyExcel.write(excelFileName).withTemplate(templeteFile).build()) {
//            WriteSheet workSheet = EasyExcel.writerSheet().sheetNo(sheetNo).build();
//
//            excelWriter.fill(new FillWrapper("data", list), workSheet);
//            excelWriter.finish();
//        }
//    }

    public static void writeExcel(List list, int sheetNo, InputStream templeteFile, String outPath, String excelName) {
        String excelFileName = outPath + excelName;
        File outputFolder = new File(outPath);
        if (!outputFolder.exists()) {
            boolean mkdirs = outputFolder.mkdirs();// 如果文件夹不存在，则创建
        }
        try (ExcelWriter excelWriter = EasyExcel.write(excelFileName).withTemplate(templeteFile).build()) {
            WriteSheet workSheet = EasyExcel.writerSheet().sheetNo(sheetNo).build();
            WriteSheet outSheet1 = EasyExcel.writerSheet().sheetNo(4).build();
            WriteSheet outSheet2 = EasyExcel.writerSheet().sheetNo(5).build();
            WriteSheet outSheet3 = EasyExcel.writerSheet().sheetNo(6).build();

            excelWriter.fill(new FillWrapper("data", list), workSheet);
            excelWriter.finish();
        } finally {
            if (templeteFile != null) {
                try {
                    templeteFile.close(); // 关闭输入流
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void writeExcel(List list, int sheetNo, String templeteFile, String outPath, String excelName) {
//        // 输出路径
//        String outPath = "C:/ExcelHandle";
//        String excelFileName = outPath + "/" + DateUtils.format(new Date(), "yyyyMMdd_HHmmss") + "处理报表.xlsx";
        String excelFileName = outPath + excelName;
        File outputFolder = new File(outPath);
        if (!outputFolder.exists()) {
            boolean mkdirs = outputFolder.mkdirs();// 如果文件夹不存在，则创建
        }
        try(ExcelWriter excelWriter = EasyExcel.write(excelFileName).withTemplate(templeteFile).build()) {
            WriteSheet workSheet = EasyExcel.writerSheet().sheetNo(sheetNo).build();

            excelWriter.fill(new FillWrapper("data", list), workSheet);
            excelWriter.finish();
        }
    }
}
